package com.ewanghuiju.app.base;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.di.component.DaggerFragmentComponent;
import com.ewanghuiju.app.di.component.FragmentComponent;
import com.ewanghuiju.app.di.module.FragmentModule;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import javax.inject.Inject;

import androidx.annotation.Nullable;

/**
 * @author sj  qq:751943509
 * @class MVP Fragment基类
 * @time 2020/2/16
 */
public abstract class BaseFragment<T extends BasePresenter> extends BaseImmersionFragment implements BaseView {

    @Inject
    protected T mPresenter;

    protected FragmentComponent getFragmentComponent() {
        return DaggerFragmentComponent.builder()
                .appComponent(App.getAppComponent())
                .fragmentModule(getFragmentModule())
                .build();
    }

    protected FragmentModule getFragmentModule() {
        return new FragmentModule(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initInject();
        mPresenter.attachView(this);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        if (mPresenter != null) mPresenter.detachView();
        super.onDestroyView();
    }

    @Override
    public void showErrorMsg(String msg, boolean isShow) {
        LoadingDialogUtils.dismissDialog_ios();
        if (isShow)
            StyleableToast.makeText(App.getInstance(), msg, Toast.LENGTH_SHORT, R.style.mytoast).show();

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    @Override
    public void stateError() {

    }

    @Override
    public void stateEmpty() {

    }

    @Override
    public void stateLoading() {

    }

    @Override
    public void stateMain() {

    }

    protected abstract void initInject();

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).keyboardEnable(true).init();
    }

}
